package logic.newLogic2;

import java.util.TreeSet;

import logic.Algorithm;
import logic.CorrelationRecAlgorithm;

import guiDisplay.RecommendationList;
import data.RatingTuple;
import data.SystemData;
import exceptions.MissingMandantoryFieldException;
import exceptions.ObjectAlreadyInitializedException;
import exceptions.UserAlreadyExistException;
import exceptions.WrongPasswordException;
import guiDisplay.MovieData;
import guiDisplay.UserData;
import guiDisplay.MovieList;


/**
 * describes the system's Facade, which delegates GUI requests
 * the Facade is a singleton
 *
 */

public class Facade3 {
	private static Facade3 _facade;

	private SystemHandler3 _sysHandler;
	private AdminHandler3 _adHandler;
	private UserHandler3 _usrHandler;
	private RatingHandler3 _ratingHandler;
	private AlgoHandler3 _algoHandler;

	private Facade3(SystemHandler3 systemHandler, AdminHandler3 adminHandler, UserHandler3 userHandler, AlgoHandler3 algoHandler, RatingHandler3 ratingHandler){
		_sysHandler = systemHandler;
		_adHandler = adminHandler;
		_usrHandler = userHandler;
		_algoHandler= algoHandler;
		_ratingHandler=ratingHandler;
	}

	public static Facade3 getInstance(){
		//TODO what to do here if facade is null?
		if (_facade==null){
			_facade = new Facade3(new SystemHandler3(),new AdminHandler3(), new UserHandler3(),new AlgoHandler3(new CorrelationRecAlgorithm()),new RatingHandler3());
		}
		return _facade;
	}

	public static void init(SystemHandler3 s, AdminHandler3 a, UserHandler3 u, Algorithm algo) throws ObjectAlreadyInitializedException{
		if (_facade==null){
			_facade = new Facade3(s, a, u, new AlgoHandler3(algo),new RatingHandler3());
		}else{
			throw new ObjectAlreadyInitializedException("Facade already initialized");
		}
	}

	public boolean login(String userName, String password) {//throws UserDoesNotExistsException{
		return _usrHandler.login(userName, password);
	}

	public void logout(/*String userName*/) {//throws UserDoesNotExistsException{
		_usrHandler.logout(/*userName*/);
	}


	public boolean deleteUser(String userName,String adminName){
		return _adHandler.deleteUser(userName, adminName);
	}

	public int registerUser(String userName, String password1, String password2, String day,String month, String year, String sex) throws WrongPasswordException, MissingMandantoryFieldException, UserAlreadyExistException{
		return _usrHandler.registerUser(userName,password1, password2, day,month,year, sex);
	}

	public int changeDetails(String name, String oldpass, String pass1, String pass2,
			String day, String month, String year, String sex) {
		return _usrHandler.changeDetails(name, oldpass, pass1, pass2, day , month, year, sex);


	}

	public boolean addMovie(String name, String category, String director, String lang ,String raters,String rating, String year,  String length, String picture) {//throws MovieAlreadyExistsException{
		return _adHandler.addMovie(name, category, director, lang, raters, rating, year, length , picture);
	}

	public boolean deleteMovie(String name){
		return _adHandler.deleteMovie(name);
	}

	public void addRating(String user , String movie, String rating , String description){

		_sysHandler.addRating(user,movie,rating,description);
	}

	public void changeRating(String user , String movie, String rating , String description){
		_sysHandler.changeRating(user,movie,rating,description);
	}

	public RecommendationList getRecommendation(String user, int start){
		return _algoHandler.setRecommendation(user,start);
	}

	public MovieData getMovieData(String text) {
		return _sysHandler.getMovieData(text);

	}

	public String[] getMovies() {
		return _sysHandler.getMovies();
	}

	public MovieList getMovies(int start) {
		return _sysHandler.getMovies(start);
	}

	public String[] getUsers(String user) {
		return _sysHandler.getUsers(user);
	}

	public String[] getRegUsers(){
		return _sysHandler.getRegUsers();
	}

	public String[] getAllUsers(){
		return _sysHandler.getAllNmaes();
	}

	public String[] getNotMngUsers(){
		return _sysHandler.getNotMngUsers();
	}

	public void exit() {
		SystemData.getInstance().exit();
	}

	public boolean seenMovie(String movieName, String userName) {
		return _usrHandler.hasSeenMovie(movieName, userName);
	}

	public void removeRating(String movieName, String userName) {
		_ratingHandler.removeRating(movieName,userName);

	}

	public int getNumRating(String user) {
		return _usrHandler.getNumRating(user);
	}

	public TreeSet<RatingTuple> getUserRatings(String user) {
		return _usrHandler.getUserRatings(user);
	}


	public String[] haveSeenRating(String user, String movie) {
		return _usrHandler.haveSeenRating(user, movie);
	}

	public boolean isAdmin(String user){
		return _usrHandler.isAdmin(user);
	}
	public boolean isManager(String user){
		return _usrHandler.isManager(user);
	}

	public UserData getUserDetails(String user){
		return SystemData.getInstance().getUserData(user);
	}
	public MovieList search(String str,int start){
		return SystemData.getInstance().search(str, start);
	}

	public void setAdmin(String name){
		_usrHandler.setAdmin(name);

	}
	public void setManager(String name){
		_usrHandler.setManager(name);

	}

}
